/*
 * Created by JFormDesigner on Sun Jun 23 12:27:53 CST 2024
 */

package scct.chenhui.forms;

import scct.chenhui.dao.BrandDao;
import scct.chenhui.model.Brand;

import java.awt.*;
import java.awt.event.*;
import java.io.DataInput;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 * @author Administrator
 */
public class PanelBrandManager extends JPanel {
    MainForm main;
    private DefaultTableModel model;

    public PanelBrandManager(MainForm main) {
        initComponents();
        this.main=main;
        initData();
    }

    private void initData() {
        model = (DefaultTableModel) tabBrand.getModel();
        model.addColumn("id");
        model.addColumn("品牌名称");
        model.addColumn("厂商");
        getTable();
    }

    private void getTable() {
        BrandDao brandDao = new BrandDao();
        List<Brand> allBrand = brandDao.getAllBrand();
        model.setRowCount(0);
        for(Brand brand :allBrand)
        {
            model.addRow(new Object[]{brand.getId(),brand.getName(),brand.getCompany()});
        }
    }


    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        AddBrandDialog dialog = new AddBrandDialog(main);
        dialog.setModal(true);
        dialog.setVisible(true);
        getTable();


    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - hui chen
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabBrand = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing
        . border. EmptyBorder( 0, 0, 0, 0) , "JF\u006frmDes\u0069gner \u0045valua\u0074ion", javax. swing. border. TitledBorder
        . CENTER, javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069alog" ,java .
        awt .Font .BOLD ,12 ), java. awt. Color. red) , getBorder( )) )
        ;  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e
        ) {if ("\u0062order" .equals (e .getPropertyName () )) throw new RuntimeException( ); }} )
        ;
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u6dfb\u52a0\u54c1\u724c");
            btnAdd.addActionListener(e -> {
					btnAdd(e);
		});
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u54c1\u724c");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabBrand);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - hui chen
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabBrand;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
